﻿Imports System.IO
Public Class clsData
    Private lDataFile As String = "Planning.xml"

    Public Function LoadData() As dsMain
        Try
            Dim lDS As New dsMain
            If File.Exists(lDataFile) Then '
                lDS.ReadXml(lDataFile)
            End If

            If lDS.Status.Rows.Count = 0 Then
                Dim lStatus() As String = {"Working", "NoWorking", "PublicHoliday", "Canceled"}
                For Each Str As String In lStatus
                    Dim dr As dsMain.StatusRow = lDS.Status.NewRow
                    dr.Status = Str
                    lDS.Status.Rows.Add(dr)
                Next
            End If
            If lDS.Systems.Rows.Count = 0 Then
                Dim lSystems() As String = {"Sm5", "SM5 Builds"}
                For Each Str As String In lSystems
                    Dim dr As dsMain.SystemsRow = lDS.Systems.NewRow
                    dr.Name = Str
                    dr.ParentID = 0
                    dr.Level = 1
                    lDS.Systems.Rows.Add(dr)
                Next
            End If

            If lDS.TimeSlotProperties.Rows.Count = 0 Then
                Dim aTimeSlotProperties(,) As String = {{"New Year's Day", "Australia Day", "Labour Day", "Good Friday", _
                                                          "Easter Monday", "ANZAC Day", "Queen's Birthday", "Melbourne Cup Day", _
                                                          "Christmas Day", "Boxing Day"}, _
                                                         {"1 Jan 2010", "26 Jan 2010", "8 Mar 2010", "2 Apr 2010", _
                                                          "5 Apr 2010", "26 Apr 2010", "14 Jun 2010", "2 Nov 2010", _
                                                          "27 Dec 2010", "28 Dec 2010"}, _
                                                         {"1", "1", "1", "1", _
                                                          "1", "1", "1", "1", _
                                                          "1", "1"}}            
                For iCtr As Integer = 0 To aTimeSlotProperties.GetUpperBound(1)
                    Dim dr As dsMain.TimeSlotPropertiesRow = lDS.TimeSlotProperties.NewRow
                    dr.Description = aTimeSlotProperties(0, iCtr)
                    dr.StartDateTime = CDate(aTimeSlotProperties(1, iCtr)).ToOADate()
                    dr.EndDateTime = DateAdd(DateInterval.Minute, 1440, CDate(aTimeSlotProperties(1, iCtr))).ToOADate()
                    dr.FullDay = CBool(aTimeSlotProperties(2, iCtr))
                    lDS.TimeSlotProperties.Rows.Add(dr)
                Next
            End If

            Return lDS
        Catch ex As Exception
            Throw ex
        End Try
    End Function

    Public Sub SaveData(ByVal pDS As dsMain)
        Try
            pDS.WriteXml(lDataFile)
        Catch ex As Exception
            Throw ex
        End Try
    End Sub
End Class
